﻿using ExpressionTree.Interface;
using ExpressionTree.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ExpressionTree
{
    public class ExpressionCore
    {
        public string ExpressionHandler<T>(Expression<Func<T, object>> exp)
        {
            return ExpressionRouting(exp);
        }
        public string ExpressionHandler1<T>(Expression<Func<T, bool>> exp)
        {
            return ExpressionRouting(exp);
        }

        private string ExpressionRouting(Expression expression)
        {
            BaseExpresstionHandler baseHandler = new DefaultExpresstionHandler();
            ExpressionHandler handler = new ExpressionHandler(baseHandler);
            baseHandler.SetExpressionHandler(handler);
            return handler.Handler(expression);
        }

        /// <summary>
        /// 获取默认解析
        /// </summary>
        /// <returns></returns>
        public static ExpressionHandler GetDefaultExpresstionHandler()
        {
            BaseExpresstionHandler baseHandler = new DefaultExpresstionHandler();
            ExpressionHandler handler = new ExpressionHandler(baseHandler);
            baseHandler.SetExpressionHandler(handler);
            return handler;
        }
        /// <summary>
        /// 获取用户自定义的解析
        /// </summary>
        /// <param name="baseHandler">继承 BaseExpresstionHandler 定制的 解析</param>
        /// <returns></returns>
        public static ExpressionHandler GetCustomExpresstionHandler(BaseExpresstionHandler baseHandler)
        {
            if (baseHandler == null) throw new Exception("BaseExpresstionHandler 不能为空");

            ExpressionHandler handler = new ExpressionHandler(baseHandler);
            baseHandler.SetExpressionHandler(handler);
            return handler;
        }

    }
}
